﻿var g_font = gdi.Font("BigNoodleTitling", 18, 0); 
var g_drag = 0;
var g_drag_seek = 0;
var g_timer, txt;

function RGB(r,g,b) {
	return (0xff000000|(r<<16)|(g<<8)|(b)); 
}

function on_size() {
	ww = window.Width;
	wh = window.Height;
}

function on_paint(gr) {
	gr.FillGradRect(0, 0, ww, wh, -90, RGB(242, 242, 242), RGB(242, 242, 255));
	if(fb.IsPlaying) {
		if(fb.PlaybackLength > 0) {
			if(g_drag){
				t = fb.PlaybackLength * g_drag_seek;
				h = Math.floor(t/3600)
				m = Math.floor((t-=h*3600)/60);
				s = Math.floor(t-=m*60);
				pos = ww * g_drag_seek;
				txt = (h > 0 ? h + ":" + (m <10 ? "0" + m : m) : m) + ":" + (s < 10 ? "0" + s : s) + " / " + length;
			} else {
				pos = ww * (fb.PlaybackTime / fb.PlaybackLength);
				txt = fb.IsPaused ? "Paused" : fb.titleformat("%playback_time%").Eval() + " / " + length;
			}
			gr.FillGradRect( 0, 0, pos, wh-1, 0, RGB(43, 51, 60), RGB(97, 185, 222));
		} else if(fb.PlaybackTime > 0.1) {
			txt = fb.IsPaused ? "Paused" : fb.titleformat("%playback_time%").Eval() + " / LIVE";
		}
		gr.SetTextRenderingHint(5);
		gr.DrawString(txt, g_font, RGB(180,0,0), 0, 0, ww, wh,0x11005000);
	}
	gr.DrawRect(0,0, ww-1, wh-1, 1.0, RGB(137,140,149));
}

function on_mouse_lbtn_down(x,y){
	if(fb.IsPlaying && fb.PlaybackLength > 0) g_drag = 1;
}

function on_mouse_lbtn_up(x,y){
	if(g_drag) {
		g_drag = 0;
		g_drag_seek = x < 0 ? 0 : x > window.Width ? 1 : x / window.Width;
		fb.PlaybackTime = fb.PlaybackLength * g_drag_seek;
	}
}

function on_mouse_move(x,y){
	if(y > 24) g_drag = 0;
	if(g_drag) {
		g_drag_seek = x < 0 ? 0 : x > window.Width ? 1 : x / window.Width;
		window.Repaint();
	}
}

function on_playback_new_track() {
	length = fb.TitleFormat("%length%").Eval();
	g_timer = window.CreateTimerInterval(100);
}

function on_playback_stop() {
	if(g_timer) window.KillTimer(g_timer);
	window.Repaint();
	CollectGarbage();
}

function on_playback_seek(time){
	window.Repaint();
}

function on_timer(id){
	window.Repaint();
}

function on_mouse_wheel(delta) {
	fb.PlaybackTime = fb.PlaybackTime + delta;
}

if(fb.isplaying) on_playback_new_track();